package org.jeecg.crawler.other;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.crawler.content.DetailContentNewsCrawler;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecgframework.p3.core.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

public class PostCrawler {


    /**
     * 中国纪检监察报
     * @return
     */
    public static List<CrawlerInfo>  getZGJJJCBao() throws Exception {
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("docPubTime", DateTimeUtil.getTodayChar8()));
        String result = HttpUtils.postForm("http://jjjcb.ccdi.gov.cn/reader/layout/findBmMenu.do", nameValuePairList);

        JSONArray jsonArray = JSONObject.parseArray(result);
        List<CrawlerInfo> resultList = new ArrayList<>();
        for (Object o : jsonArray) {
            JSONObject content = JSONObject.parseObject(o.toString());
            String layLout = content.getString("BC")+  content.getString("BM");
             String num =   content.getString("IRCATELOG");
             String sourceUrl = "http://jjjcb.ccdi.gov.cn/epaper/";
             String sourceName = "中国纪检监察报";
            List<NameValuePair> detailValuePairList = new ArrayList<>();

            detailValuePairList.add(new BasicNameValuePair("docpubtime", DateTimeUtil.getTodayChar8En()));
            detailValuePairList.add(new BasicNameValuePair("bc", num));

            String detail = HttpUtils.postForm("http://jjjcb.ccdi.gov.cn/reader/layout/getBmDetail.do", detailValuePairList);
            JSONArray detailJsonArray = JSONObject.parseArray(detail);
            for (Object o1 : detailJsonArray) {
                JSONObject detailJson = JSONObject.parseObject(o1.toString());
                String zb_guid = detailJson.getString("ZB_GUID");
                CrawlerInfo crawlerInfo = new CrawlerInfo();
                crawlerInfo.setArticleTime( DateTimeUtil.getTodayChar8En());
                crawlerInfo.setArticleName(detailJson.getString("DOCTITLE"));
                crawlerInfo.setLayout(layLout);
                crawlerInfo.setScoureUrl(sourceUrl);
                crawlerInfo.setArticleUrl("http://jjjcb.ccdi.gov.cn/epaper/index.html?guid="+zb_guid);
                crawlerInfo.setId(zb_guid);
                crawlerInfo.setArticleContent(DetailContentNewsCrawler.getHtmlByUrl(crawlerInfo.getArticleUrl()));
                crawlerInfo.setSourceName(sourceName);
                resultList.add(crawlerInfo);
            }

        }
        return  resultList;
    }

    public static void main(String[] args) throws Exception {
        getZGJJJCBao();
    }
}
